{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "DJeeABnkOsPK"
      },
      "source": [
        "# Graph RAG Cookbook"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9fHzgxYpmz2q"
      },
      "source": [
        "You can also check this cookbook in colab [here](https://colab.research.google.com/drive/1uZKQSuu0qW6ukkuSv9TukLB9bVaS1H0U?usp=sharing)\n",
        "\n",
        "⭐ <i>Star us on [*Github*](https://github.com/camel-ai/camel), join our [*Discord*](https://discord.camel-ai.org) or follow our [*X*](https://x.com/camelaiorg)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "l4VANToobUzu"
      },
      "source": [
        "This cookbook walks you through the process of performing graph-based Retrieval-Augmented Generation (RAG) using **CAMEL**, powered by the advanced **Mistral** models. Specifically, we'll utilize the **Mistral Large 2** model to extract and structure knowledge from a given content source, and store this information in a **Neo4j** graph database. Subsequently, we can leverage a hybrid approach, combining vector retrieval and knowledge graph retrieval, to query and explore the stored knowledge."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "slaeSyWkbIzc"
      },
      "source": [
        "![Slide 16_9 - 9.png]()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "X5mWln9dCDzE"
      },
      "source": [
        "![Screenshot 2024-07-25 at 21.14.27.png]()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8SV7XmyKbwdc"
      },
      "source": [
        "## 📦 Installation"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-qAA1j0Sb2Ve"
      },
      "source": [
        "First, install the CAMEL package with all its dependencies:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "os7Mcfw2Svww"
      },
      "outputs": [],
      "source": [
        "pip install \"camel-ai[all]==0.2.16\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "X5SDX1kPb6bz"
      },
      "source": [
        "## 🔧 Setup"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "StvpXTi7b7pF"
      },
      "source": [
        "Import the required modules from CAMEL-AI:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "7PcVsBYvzfSI"
      },
      "outputs": [],
      "source": [
        "from camel.models import ModelFactory\n",
        "from camel.types import ModelPlatformType, ModelType\n",
        "from camel.configs import MistralConfig, OllamaConfig\n",
        "from camel.loaders import UnstructuredIO\n",
        "from camel.storages import Neo4jGraph\n",
        "from camel.retrievers import AutoRetriever\n",
        "from camel.embeddings import MistralEmbedding\n",
        "from camel.types import StorageType\n",
        "from camel.agents import ChatAgent, KnowledgeGraphAgent\n",
        "from camel.messages import BaseMessage"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DahHkqSOb-T3"
      },
      "source": [
        "## 🔑 Setting Up API Keys\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "odHOpz0XcAjA"
      },
      "source": [
        "For secure access to Mistral AI's services, we'll prompt for the API key."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "B0v7xEFFoabv",
        "outputId": "7cf90833-d7ab-49a0-fda9-46c99af7ccc6"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Enter your API key: ··········\n"
          ]
        }
      ],
      "source": [
        "import os\n",
        "from getpass import getpass\n",
        "\n",
        "# Prompt for the API key securely\n",
        "mistral_api_key = getpass('Enter your API key: ')\n",
        "os.environ[\"MISTRAL_API_KEY\"] = mistral_api_key"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Alternatively, if running on Colab, you could save your API keys and tokens as **Colab Secrets**, and use them across notebooks.\n",
        "\n",
        "To do so, **comment out** the above **manual** API key prompt code block(s), and **uncomment** the following codeblock.\n",
        "\n",
        "⚠️ Don't forget granting access to the API key you would be using to the current notebook."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# import os\n",
        "# from google.colab import userdata\n",
        "\n",
        "# os.environ[\"MISTRAL_API_KEY\"] = userdata.get(\"MISTRAL_API_KEY\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bPPZx6WwcDzG"
      },
      "source": [
        "## 🗄️ Configuring Neo4j Graph Database"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "m4pJ-V2FcG6o"
      },
      "source": [
        "Set up your Neo4j instance by providing the URL, username, and password, [here](https://neo4j.com/docs/aura/auradb/getting-started/create-database/) is the guidance, check your credentials in the downloaded .txt file. Note that you may need to wait up to 60 seconds if the instance has just been set up."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "iH7NuLvq3p2o"
      },
      "outputs": [],
      "source": [
        "# Set Neo4j instance\n",
        "n4j = Neo4jGraph(\n",
        "    url=\"Your_URL\",\n",
        "    username=\"Your_USERNAME\",\n",
        "    password=\"Your_PASSWORD\",\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vmzruZl1cJQD"
      },
      "source": [
        "## 🧠 Creating the Model"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "flTRxefgcOxp"
      },
      "source": [
        "Set up Mistral Large 2 model using the CAMEL ModelFactory:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "PDhfqzA2zYmm"
      },
      "outputs": [],
      "source": [
        "# Set up model\n",
        "mistral_large_2 = ModelFactory.create(\n",
        "    model_platform=ModelPlatformType.MISTRAL,\n",
        "    model_type=ModelType.MISTRAL_LARGE,\n",
        "    model_config_dict=MistralConfig(temperature=0.2).as_dict(),\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "PPyHfsGyraJf"
      },
      "outputs": [],
      "source": [
        "# You can also set up model locally by using ollama\n",
        "mistral_large_2_local = ModelFactory.create(\n",
        "    model_platform=ModelPlatformType.OLLAMA,\n",
        "    model_type=\"mistral-large\",\n",
        "    model_config_dict=OllamaConfig(temperature=0.2).as_dict(),\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "61PwBl9XcWJ_"
      },
      "source": [
        "## 🤖 Generate a Knowledge Graph Using CAMEL's Agent"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nFl_Paah4OdP"
      },
      "source": [
        "Set up instances for knowledge graph agent:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "LkUn3Ph0S1Z0"
      },
      "outputs": [],
      "source": [
        "# Set instance\n",
        "uio = UnstructuredIO()\n",
        "kg_agent = KnowledgeGraphAgent(model=mistral_large_2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "L7EUMC-nck0d"
      },
      "source": [
        "Provide an example text input that the knowledge graph agent will process:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "syY548PW4d2t"
      },
      "outputs": [],
      "source": [
        "# Set example text input\n",
        "text_example = \"\"\"\n",
        "CAMEL has developed a knowledge graph agent can run with Mistral AI's most\n",
        "advanced model, the Mistral Large 2. This knowledge graph agent is capable\n",
        "of extracting entities and relationships from given content and create knowledge\n",
        "graphs automatically.\n",
        "\"\"\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bKSxhv0ucof9"
      },
      "source": [
        "Create an element from the text and use the knowledge graph agent to extract node and relationship information:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "id": "cDB_iDCl4h6F"
      },
      "outputs": [],
      "source": [
        "# Create an element from given text\n",
        "element_example = uio.create_element_from_text(\n",
        "    text=text_example, element_id=\"0\"\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WlmeLofo4kDE",
        "outputId": "1543028f-58cd-417c-b9f1-ee28b5ebdcd7"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Sure, let's go through the process of extracting nodes and relationships from the given content and structuring them into Node and Relationship objects.\n",
            "\n",
            "### Content:\n",
            "\"CAMEL has developed a knowledge graph agent that can run with Mistral AI's most advanced model, the Mistral Large 2. This knowledge graph agent is capable of extracting entities and relationships from given content and create knowledge graphs automatically.\"\n",
            "\n",
            "### Step-by-Step Extraction:\n",
            "\n",
            "1. **Identify Entities (Nodes):**\n",
            "   - CAMEL\n",
            "   - knowledge graph agent\n",
            "   - Mistral AI\n",
            "   - Mistral Large 2\n",
            "   - entities\n",
            "   - relationships\n",
            "   - content\n",
            "   - knowledge graphs\n",
            "\n",
            "2. **Categorize Entities:**\n",
            "   - CAMEL: Organization\n",
            "   - knowledge graph agent: Software\n",
            "   - Mistral AI: Organization\n",
            "   - Mistral Large 2: Model\n",
            "   - entities: Concept\n",
            "   - relationships: Concept\n",
            "   - content: Concept\n",
            "   - knowledge graphs: Concept\n",
            "\n",
            "3. **Identify Relationships:**\n",
            "   - CAMEL developed knowledge graph agent\n",
            "   - knowledge graph agent can run with Mistral AI's Mistral Large 2\n",
            "   - knowledge graph agent is capable of extracting entities and relationships from content\n",
            "   - knowledge graph agent creates knowledge graphs\n",
            "\n",
            "### Structured Output:\n",
            "\n",
            "#### Nodes:\n",
            "```python\n",
            "Node(id='CAMEL', type='Organization')\n",
            "Node(id='knowledge graph agent', type='Software')\n",
            "Node(id='Mistral AI', type='Organization')\n",
            "Node(id='Mistral Large 2', type='Model')\n",
            "Node(id='entities', type='Concept')\n",
            "Node(id='relationships', type='Concept')\n",
            "Node(id='content', type='Concept')\n",
            "Node(id='knowledge graphs', type='Concept')\n",
            "```\n",
            "\n",
            "#### Relationships:\n",
            "```python\n",
            "Relationship(subj=Node(id='CAMEL', type='Organization'), obj=Node(id='knowledge graph agent', type='Software'), type='Developed')\n",
            "Relationship(subj=Node(id='knowledge graph agent', type='Software'), obj=Node(id='Mistral AI', type='Organization'), type='RunsWith')\n",
            "Relationship(subj=Node(id='knowledge graph agent', type='Software'), obj=Node(id='Mistral Large 2', type='Model'), type='RunsWith')\n",
            "Relationship(subj=Node(id='knowledge graph agent', type='Software'), obj=Node(id='entities', type='Concept'), type='Extracts')\n",
            "Relationship(subj=Node(id='knowledge graph agent', type='Software'), obj=Node(id='relationships', type='Concept'), type='Extracts')\n",
            "Relationship(subj=Node(id='knowledge graph agent', type='Software'), obj=Node(id='content', type='Concept'), type='ExtractsFrom')\n",
            "Relationship(subj=Node(id='knowledge graph agent', type='Software'), obj=Node(id='knowledge graphs', type='Concept'), type='Creates')\n",
            "```\n",
            "\n",
            "This structured output captures the entities and their relationships as described in the content. Each node has a unique identifier and a type, and each relationship specifies the subject, object, and type of the relationship.\n"
          ]
        }
      ],
      "source": [
        "# Let Knowledge Graph Agent extract node and relationship information\n",
        "ans_element = kg_agent.run(element_example, parse_graph_elements=False)\n",
        "print(ans_element)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "J0JzGaFN4pRh",
        "outputId": "6e957343-1388-4e1b-eefa-8cdaa117eff4"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "nodes=[Node(id='CAMEL', type='Organization', properties={'source': 'agent_created'}), Node(id='knowledge graph agent', type='Software', properties={'source': 'agent_created'}), Node(id='Mistral AI', type='Organization', properties={'source': 'agent_created'}), Node(id='Mistral Large 2', type='Model', properties={'source': 'agent_created'}), Node(id='entities and relationships', type='Concept', properties={'source': 'agent_created'}), Node(id='knowledge graphs', type='Concept', properties={'source': 'agent_created'})] relationships=[Relationship(subj=Node(id='CAMEL', type='Organization', properties={'source': 'agent_created'}), obj=Node(id='knowledge graph agent', type='Software', properties={'source': 'agent_created'}), type='Developed', properties={'source': 'agent_created'}), Relationship(subj=Node(id='knowledge graph agent', type='Software', properties={'source': 'agent_created'}), obj=Node(id='Mistral AI', type='Organization', properties={'source': 'agent_created'}), type='CanRunWith', properties={'source': 'agent_created'}), Relationship(subj=Node(id='Mistral AI', type='Organization', properties={'source': 'agent_created'}), obj=Node(id='Mistral Large 2', type='Model', properties={'source': 'agent_created'}), type='HasModel', properties={'source': 'agent_created'}), Relationship(subj=Node(id='knowledge graph agent', type='Software', properties={'source': 'agent_created'}), obj=Node(id='entities and relationships', type='Concept', properties={'source': 'agent_created'}), type='CanExtract', properties={'source': 'agent_created'}), Relationship(subj=Node(id='knowledge graph agent', type='Software', properties={'source': 'agent_created'}), obj=Node(id='knowledge graphs', type='Concept', properties={'source': 'agent_created'}), type='CanCreate', properties={'source': 'agent_created'})] source=<unstructured.documents.elements.Text object at 0x785a8686f6a0>\n"
          ]
        }
      ],
      "source": [
        "# Check graph element\n",
        "graph_elements = kg_agent.run(element_example, parse_graph_elements=True)\n",
        "print(graph_elements)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sqEfkLI-c23g"
      },
      "source": [
        "Add the extracted graph elements to the Neo4j database:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "id": "89ZF99QSAaGJ"
      },
      "outputs": [],
      "source": [
        "# Add the element to neo4j database\n",
        "n4j.add_graph_elements(graph_elements=[graph_elements])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "B6C8wSo6x27Z"
      },
      "source": [
        "### 🎉 Now you can go to [here](https://workspace-preview.neo4j.io/connection/connect) to check the knowledge graph built with CAMEL's Knowledge Graph Agent and Mistral AI's Mistral Large 2 model!"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LckRFGpN6Za4"
      },
      "source": [
        "## 🗃️ Running Graph RAG with CAMEL"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vh4jrhPSyaZw"
      },
      "source": [
        "*Next we will showcase how to run RAG in a hybrid approach, combining vector retrieval and knowledge graph retrieval, to query and explore the stored knowledge.*"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "A_MiPGmOeDN3"
      },
      "source": [
        "Set up a vector retriever with local storage and embedding model from Mistral AI:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "AwpMPpvtr6O7"
      },
      "outputs": [],
      "source": [
        "# Set retriever\n",
        "camel_retriever = AutoRetriever(\n",
        "    vector_storage_local_path=\"local_data/embedding_storage\",\n",
        "    storage_type=StorageType.QDRANT,\n",
        "    embedding_model=MistralEmbedding(),\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BpAPOf7VeFjx"
      },
      "source": [
        "Provide an example user query:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "id": "PTZ-8nX359bb"
      },
      "outputs": [],
      "source": [
        "# Set one user query\n",
        "query=\"what's the relationship between Mistral Large 2 and Mistral AI? What kind of feature does Mistral Large 2 has?\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Yq6kGaEJeI53"
      },
      "source": [
        "Retrieve related content using the vector retriever, here we take Mistral AI's news in the website as example content, you can also set the local file path here:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hLQGgp4DJAlu",
        "outputId": "03b8fa1a-ddcd-45ae-f3e7-6c06121504e0"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{'Original Query': \"what's the relationship between Mistral Large 2 and Mistral AI? What kind of feature does Mistral Large 2 has?\", 'Retrieved Context': ['Large Enough\\n\\nToday, we are announcing Mistral Large 2, the new generation of our flagship model. Compared to its predecessor, Mistral Large 2 is significantly more capable in code generation, mathematics, and reasoning. It also provides a much stronger multilingual support, and advanced function calling capabilities.\\n\\nJuly 24, 2024\\n\\nMistral AI team']}\n"
          ]
        }
      ],
      "source": [
        "# Get related content by using vector retriever\n",
        "vector_result = camel_retriever.run_vector_retriever(\n",
        "    query=query,\n",
        "    contents=\"https://mistral.ai/news/mistral-large-2407/\",\n",
        ")\n",
        "\n",
        "# Show the result from vector search\n",
        "print(vector_result)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "w188PLzteMkw"
      },
      "source": [
        "Parse content from the specified URL and create knowledge graph data:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "DyuusYoo5-Vw"
      },
      "outputs": [],
      "source": [
        "# Parse content from mistral website and create knowledge graph data by using\n",
        "# the Knowledge Graph Agent, store the information into graph database.\n",
        "\n",
        "elements = uio.parse_file_or_url(\n",
        "    input_path=\"https://mistral.ai/news/mistral-large-2407/\"\n",
        ")\n",
        "chunk_elements = uio.chunk_elements(\n",
        "    chunk_type=\"chunk_by_title\", elements=elements\n",
        ")\n",
        "\n",
        "graph_elements = []\n",
        "for chunk in chunk_elements:\n",
        "    graph_element = kg_agent.run(chunk, parse_graph_elements=True)\n",
        "    n4j.add_graph_elements(graph_elements=[graph_element])\n",
        "    graph_elements.append(graph_element)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1UgtR5UWeSD0"
      },
      "source": [
        "Create an element from the user query:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "id": "ZDYCLClxeT68"
      },
      "outputs": [],
      "source": [
        "# Create an element from user query\n",
        "query_element = uio.create_element_from_text(\n",
        "    text=query, element_id=\"1\"\n",
        ")\n",
        "\n",
        "# Let Knowledge Graph Agent extract node and relationship information from the qyery\n",
        "ans_element = kg_agent.run(query_element, parse_graph_elements=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zD9hH7QAeVNT"
      },
      "source": [
        "Match entities from the query in the knowledge graph storage content:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ttL5pnye66Wl",
        "outputId": "d7714d35-ca22-4cc9-ab4a-92b67fbc1ecf"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "['Node Mistral Large 2 (label: Model) has relationship SUCCESSOROF with Node Mistral Large (label: Model)', 'Node Mistral Large 2 (label: Model) has relationship ANNOUNCEDBY with Node Mistral AI team (label: Organization)', 'Node Mistral Large 2 (label: Model) has relationship HASCAPABILITY with Node code generation (label: Capability)', 'Node Mistral Large 2 (label: Model) has relationship HASCAPABILITY with Node mathematics (label: Capability)', 'Node Mistral Large 2 (label: Model) has relationship HASCAPABILITY with Node reasoning (label: Capability)', 'Node Mistral Large 2 (label: Model) has relationship HASCAPABILITY with Node Instruction-following capabilities (label: Capability)', 'Node Mistral Large 2 (label: Model) has relationship HASCAPABILITY with Node Conversational capabilities (label: Capability)', 'Node Mistral Large 2 (label: Model) has relationship PROVIDES with Node multilingual support (label: Capability)', 'Node Mistral Large 2 (label: Model) has relationship PROVIDES with Node advanced function calling capabilities (label: Capability)', 'Node Mistral Large 2 (label: Model) has relationship ANNOUNCEDON with Node July 24, 2024 (label: Date)', 'Node Mistral Large 2 (label: Model) has relationship HASFEATURE with Node 128k context window (label: Feature)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node French (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node German (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node Spanish (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node Italian (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node Portuguese (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node Arabic (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node Hindi (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node Russian (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node Chinese (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node Japanese (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSLANGUAGE with Node Korean (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSCODINGLANGUAGE with Node Python (label: CodingLanguage)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSCODINGLANGUAGE with Node Java (label: CodingLanguage)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSCODINGLANGUAGE with Node C (label: CodingLanguage)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSCODINGLANGUAGE with Node C++ (label: CodingLanguage)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSCODINGLANGUAGE with Node JavaScript (label: CodingLanguage)', 'Node Mistral Large 2 (label: Model) has relationship SUPPORTSCODINGLANGUAGE with Node Bash (label: CodingLanguage)', 'Node Mistral Large 2 (label: Model) has relationship DESIGNEDFOR with Node Single-node inference (label: Concept)', 'Node Mistral Large 2 (label: Model) has relationship DESIGNEDFOR with Node Long-context applications (label: Concept)', 'Node Mistral Large 2 (label: Model) has relationship HAS with Node 123 billion parameters (label: Attribute)', 'Node Mistral Large 2 (label: Model) has relationship ALLOWS with Node Large throughput (label: Attribute)', 'Node Mistral Large 2 (label: Model) has relationship RELEASEDUNDER with Node Mistral Research License (label: License)', 'Node Mistral Large 2 (label: Model) has relationship SETSFRONTIERIN with Node Performance (label: Metric)', 'Node Mistral Large 2 (label: Model) has relationship SETSFRONTIERIN with Node Cost of serving (label: Metric)', 'Node Mistral Large 2 (label: Model) has relationship SETSFRONTIERON with Node Evaluation metrics (label: Concept)', 'Node Mistral Large 2 (label: Model) has relationship SETSNEWPOINTON with Node Performance/cost Pareto front (label: Concept)', 'Node Mistral Large 2 (label: Model) has relationship RELATEDTO with Node Open models (label: Concept)', 'Node Mistral Large 2 (label: Model) has relationship ISBETTERAT with Node Precise instructions (label: Concept)', 'Node Mistral Large 2 (label: Model) has relationship ISBETTERAT with Node Long multi-turn conversations (label: Concept)', 'Node Mistral Large 2 (label: Model) has relationship ISEVALUATEDON with Node MT-Bench (label: Benchmark)', 'Node Mistral Large 2 (label: Model) has relationship ISEVALUATEDON with Node Wild Bench (label: Benchmark)', 'Node Mistral Large 2 (label: Model) has relationship ISEVALUATEDON with Node Arena Hard benchmarks (label: Benchmark)', 'Node Mistral Large 2 (label: Model) has relationship ISEVALUATEDON with Node General alignment benchmarks (label: Benchmark)', 'Node Mistral Large 2 (label: Model) has relationship TRAINEDON with Node Multilingual Data (label: Concept)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node French (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node German (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Spanish (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Italian (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Portuguese (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Arabic (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Hindi (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Russian (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Chinese (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Japanese (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Korean (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node English (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship EXCELSIN with Node Dutch (label: Language)', 'Node Mistral Large 2 (label: Model) has relationship HASPERFORMANCERESULTSON with Node MMLU Benchmark (label: Benchmark)', 'Node Mistral Large 2 (label: Model) has relationship COMPAREDTO with Node Mistral Large (label: Model)', 'Node Mistral Large 2 (label: Model) has relationship COMPAREDTO with Node Llama (label: Model)', 'Node Mistral Large 2 (label: Model) has relationship EQUIPPEDWITH with Node enhanced function calling and retrieval skills (label: Skill)', 'Node Mistral Large 2 (label: Model) has relationship TRAINEDTOEXECUTE with Node parallel and sequential function calls (label: Functionality)', 'Node Mistral Large 2 (label: Model) has relationship SERVESASPOWERENGINE with Node complex business applications (label: Application)', 'Node Mistral Large 2 (label: Technology) has relationship EXPOSEDON with Node la Plateforme (label: Platform)', 'Node Mistral Large 2 (label: Technology) has relationship ENRICHEDWITH with Node new features (label: Feature)', 'Node Mistral AI (label: Organization) has relationship PROVIDES with Node Mistral Large 2 (label: Model)', 'Node Mistral AI (label: Organization) has relationship HAS with Node Legal notice (label: Document)', 'Node Mistral AI (label: Organization) has relationship PARTNERSWITH with Node Google Cloud Platform (label: Cloud Service Provider)', 'Node Mistral AI (label: Organization) has relationship PROVIDESMODELSON with Node Vertex AI (label: Platform)', 'Node Mistral AI (label: Organization) has relationship PROVIDESMODELSON with Node Azure AI Studio (label: Platform)', 'Node Mistral AI (label: Organization) has relationship PROVIDESMODELSON with Node Amazon Bedrock (label: Platform)', 'Node Mistral AI (label: Organization) has relationship PROVIDESMODELSON with Node IBM watsonx.ai (label: Platform)']\n"
          ]
        }
      ],
      "source": [
        "# Match the entity got from query in the knowledge graph storage content\n",
        "kg_result = []\n",
        "for node in ans_element.nodes:\n",
        "    n4j_query = f\"\"\"\n",
        "MATCH (n {{id: '{node.id}'}})-[r]->(m)\n",
        "RETURN 'Node ' + n.id + ' (label: ' + labels(n)[0] + ') has relationship ' + type(r) + ' with Node ' + m.id + ' (label: ' + labels(m)[0] + ')' AS Description\n",
        "UNION\n",
        "MATCH (n)<-[r]-(m {{id: '{node.id}'}})\n",
        "RETURN 'Node ' + m.id + ' (label: ' + labels(m)[0] + ') has relationship ' + type(r) + ' with Node ' + n.id + ' (label: ' + labels(n)[0] + ')' AS Description\n",
        "\"\"\"\n",
        "    result = n4j.query(query=n4j_query)\n",
        "    kg_result.extend(result)\n",
        "\n",
        "kg_result = [item['Description'] for item in kg_result]\n",
        "\n",
        "# Show the result from knowledge graph database\n",
        "print(kg_result)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sjKr29w8eZ4n"
      },
      "source": [
        "Combine results from the vector search and knowledge graph entity search:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "id": "P9ne7UoB6GY5"
      },
      "outputs": [],
      "source": [
        "# combine result from vector search and knowledge graph entity search\n",
        "comined_results = str(vector_result) + \"\\n\".join(kg_result)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "q-NaDHypebKZ"
      },
      "source": [
        "Set up an assistant agent to answer questions based on the retrieved context:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LxEUi36F7kq1",
        "outputId": "1387ff6d-52f3-4f23-d1b7-4a66b0806264"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Mistral Large 2 is a model developed and provided by Mistral AI. It is the successor to the original Mistral Large model. Mistral Large 2 has several notable features and capabilities, including:\n",
            "\n",
            "1. **Capabilities**:\n",
            "   - Code generation\n",
            "   - Mathematics\n",
            "   - Reasoning\n",
            "   - Instruction-following capabilities\n",
            "   - Conversational capabilities\n",
            "\n",
            "2. **Multilingual Support**:\n",
            "   - Provides strong multilingual support for languages such as French, German, Spanish, Italian, Portuguese, Arabic, Hindi, Russian, Chinese, Japanese, Korean, English, and Dutch.\n",
            "\n",
            "3. **Advanced Function Calling Capabilities**:\n",
            "   - Equipped with enhanced function calling and retrieval skills.\n",
            "   - Trained to execute parallel and sequential function calls.\n",
            "\n",
            "4. **Context Window**:\n",
            "   - Has a 128k context window.\n",
            "\n",
            "5. **Coding Language Support**:\n",
            "   - Supports coding languages like Python, Java, C, C++, JavaScript, and Bash.\n",
            "\n",
            "6. **Design**:\n",
            "   - Designed for single-node inference and long-context applications.\n",
            "\n",
            "7. **Attributes**:\n",
            "   - Has 123 billion parameters.\n",
            "   - Allows large throughput.\n",
            "\n",
            "8. **Licensing**:\n",
            "   - Released under the Mistral Research License.\n",
            "\n",
            "9. **Performance**:\n",
            "   - Sets a new frontier in performance, cost of serving, and evaluation metrics.\n",
            "   - Sets a new point on the performance/cost Pareto front.\n",
            "\n",
            "10. **Evaluation**:\n",
            "    - Evaluated on benchmarks such as MT-Bench, Wild Bench, Arena Hard benchmarks, and General alignment benchmarks.\n",
            "\n",
            "11. **Training Data**:\n",
            "    - Trained on multilingual data.\n",
            "\n",
            "12. **Applications**:\n",
            "    - Serves as a powerful engine for complex business applications.\n",
            "\n",
            "13. **Platforms**:\n",
            "    - Exposed on platforms like la Plateforme.\n",
            "    - Enriched with new features.\n",
            "\n",
            "Mistral AI, the organization, provides Mistral Large 2 and partners with various cloud service providers like Google Cloud Platform, Vertex AI, Azure AI Studio, Amazon Bedrock, and IBM watsonx.ai to offer their models.\n"
          ]
        }
      ],
      "source": [
        "# Set agent\n",
        "sys_msg = BaseMessage.make_assistant_message(\n",
        "    role_name=\"CAMEL Agent\",\n",
        "    content=\"\"\"You are a helpful assistant to answer question,\n",
        "        I will give you the Original Query and Retrieved Context,\n",
        "    answer the Original Query based on the Retrieved Context.\"\"\",\n",
        ")\n",
        "\n",
        "camel_agent = ChatAgent(system_message=sys_msg,\n",
        "                        model=mistral_large_2)\n",
        "\n",
        "# Pass the retrieved information to agent\n",
        "user_prompt=f\"\"\"\n",
        "The Original Query is {query}\n",
        "The Retrieved Context is {comined_results}\n",
        "\"\"\"\n",
        "\n",
        "user_msg = BaseMessage.make_user_message(\n",
        "    role_name=\"CAMEL User\", content=user_prompt\n",
        ")\n",
        "\n",
        "# Get response\n",
        "agent_response = camel_agent.step(user_msg)\n",
        "\n",
        "print(agent_response.msg.content)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZMWitnLdlYCT"
      },
      "source": [
        "## 🌟 Highlights"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "w2OwDpJ0lg8o"
      },
      "source": [
        "*   Automated Knowledge Extraction: The Knowledge Graph Agent automates the extraction of entities and relationships, making the process efficient and effective.\n",
        "\n",
        "*   Mistral AI Integration: This cookbook showcases the integration of Mistral AI's advanced models, particularly the Mistral Large 2, with CAMEL-AI to create a powerful knowledge graph system.\n",
        "\n",
        "\n",
        "*   Secure and Scalable: Using CAMEL-AI's robust architecture and Neo4j for graph storage ensures that the solution is both secure and scalable."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tlTJulaQlbyW"
      },
      "source": [
        "By following this cookbook, you can leverage the cutting-edge capabilities of **CAMEL AI** and **Mistral AI** to build sophisticated knowledge graphs, facilitating advanced data analysis and retrieval tasks."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
